/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import Ajv, { ValidateFunction } from 'ajv';
import fse from 'fs-extra';

/**
 * schema校验函数存储
 * 维护schema校验函数
 *
 * @since 2022/9/14
 */
export class ValidatorStore {
  private static validatorMap: Map<string, ValidateFunction> = new Map();

  /**
   * 获取校验函数
   *
   * @param key 函数id
   * @returns {ValidateFunction | undefined} 找到的校验函数
   */
  static getValidator(key: string): ValidateFunction | undefined {
    return ValidatorStore.validatorMap.get(key);
  }

  /**
   * 判断校验函数是否存在
   *
   * @param key 函数id
   * @returns {boolean} 判断结果
   */
  static hasValidator(key: string): boolean {
    return ValidatorStore.validatorMap.has(key);
  }

  /**
   * 添加校验函数
   *
   * @param schemaPath schema路径
   * @param changeAppSchema 是否需要修改app.json5的schema中的properties.app.required
   * @returns {ValidateFunction} 添加的校验函数，如果schemaPath和changeAppSchema对应的校验函数已存在，就返回已有的校验函数
   */
  static addValidator(schemaPath: string, changeAppSchema = false): ValidateFunction {
    const key = `${schemaPath}:${changeAppSchema}`;
    if (ValidatorStore.hasValidator(key)) {
      return ValidatorStore.getValidator(key)!;
    }
    const schema = fse.readJSONSync(schemaPath);
    if (changeAppSchema) {
      schema.properties.app.required = ['bundleName', 'versionCode', 'versionName'];
    }
    const ajv = new Ajv({ strict: false, allErrors: true });
    const validator: ValidateFunction = ajv.compile(schema);
    ValidatorStore.validatorMap.set(key, validator);
    return validator;
  }

  static clear() {
    ValidatorStore.validatorMap.clear();
  }

  /**
   * 提供获取schema校验函数存储map,仓颉覆盖map中的校验函数
   * @returns {Map<string, >} schema校验函数存储map
   */
  static getValidatorMap(): Map<string, ValidateFunction> {
    return ValidatorStore.validatorMap;
  }
}
